package com.itheima.health.service.impl;


import com.itheima.health.dao.OrderPersonDao;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.dao.SetmealDao;
import com.itheima.health.dto.OrderSubmitParam;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.OrderPersonService;
import com.itheima.health.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;


@Service
public class OrderPersonServiceImpl implements OrderPersonService {
    @Autowired
    private OrderPersonDao orderPersonDao;

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private SetmealDao  setmealDao;

    @Override
    public void add(OrderSubmitParam orderSubmitParam) {



            orderPersonDao.insert(orderSubmitParam);
            Member  member = Member.builder().name(orderSubmitParam.getName())
                                             .sex(orderSubmitParam.getSex())
                                             .idCard(orderSubmitParam.getIdCard())
                                             .phoneNumber(orderSubmitParam.getTelephone())
                                             .regTime(LocalDate.now()).build();
            orderPersonDao.insertHY(member);
            orderSettingDao.update(orderSubmitParam.getOrderDate());

    }

    @Override
    public OrderSubmitParam getOrderByIdCardAndOrderDate(String idCard, Date orderDate) {

      OrderSubmitParam  person  = orderPersonDao.getOrderByIdCardAndOrderDate(idCard,orderDate);
        return person;
    }

    @Override
    public OrderVO findById(@PathVariable Integer orderId) {
        OrderSubmitParam orderSubmit = orderPersonDao.findById(orderId);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String orderDate=sdf.format(orderSubmit.getOrderDate());
        OrderVO  order =  OrderVO.builder()
                .member(orderSubmit.getName())
                .setmeal(setmealDao.findById(orderSubmit.getSetMealId()).getName())
                .orderDate(orderDate)
                .orderType(orderSubmit.getOrderType())
                .build();
        return order;
    }


}
